//
//  ViewController.m
//  Masonry设置优先级
//
//  Created by 梁森 on 2018/12/16.
//  Copyright © 2018年 梁森. All rights reserved.
//

#import "ViewController.h"

#import <Masonry.h>

@interface ViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) UILabel * leftlbl;
@property (nonatomic, strong) UILabel * rightLbl;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.view.backgroundColor = [UIColor greenColor];
    
    UILabel * leftlbl = [UILabel new];
    [self.view addSubview:leftlbl];
    UILabel * rightLbl = [UILabel new];
    [self.view addSubview:rightLbl];
    
    leftlbl.backgroundColor = [UIColor yellowColor];
    rightLbl.backgroundColor = [UIColor redColor];
    
    [rightLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(100);
        make.right.equalTo(self.view.mas_right).offset(-10);
        make.width.mas_greaterThanOrEqualTo(10);   // 这是最小宽度
    }];
    [leftlbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(10);
        make.top.equalTo(self.view.mas_top).offset(100);
        make.right.equalTo(rightLbl.mas_left).offset(-10);
        make.width.mas_greaterThanOrEqualTo(10);
    }];
    
    UITextField * textField1 = [[UITextField alloc] initWithFrame:CGRectMake(10, 60, 100, 30)];
    [self.view addSubview:textField1];
    textField1.placeholder = @"请输入...";
    textField1.backgroundColor = [UIColor whiteColor];
    textField1.textColor = [UIColor blackColor];
    textField1.delegate = self;
    [textField1 addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
    textField1.tag = 1;
    UITextField * textField2 = [[UITextField alloc] initWithFrame:CGRectMake(120, 60, 100, 30)];
    [self.view addSubview:textField2];
    textField2.placeholder = @"请输入...";
    textField2.backgroundColor = [UIColor whiteColor];
    textField2.textColor = [UIColor blackColor];
    textField2.delegate = self;
    [textField2 addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
    textField2.tag = 2;
    
    
    leftlbl.text = textField1.text;
    rightLbl.text = textField2.text;
    // 设置抗压缩优先级
    [leftlbl setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
    [rightLbl setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
    
    UILabel * blueLbl = [UILabel new];
    [self.view addSubview:blueLbl];
    blueLbl.backgroundColor = [UIColor blueColor];
    blueLbl.text = @"文字文字文字文字-";
    [blueLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(leftlbl);
        make.top.equalTo(leftlbl.mas_bottom).offset(10);
        make.width.equalTo(@(100));
    }];
    
    
    UILabel * orangeLbl = [UILabel new];
    orangeLbl.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:orangeLbl];
    orangeLbl.text = @"设置最小宽度-";
    [orangeLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(10);
        make.top.equalTo(self.view.mas_top).offset(200);
        make.width.mas_greaterThanOrEqualTo(self.view.frame.size.width * 0.5 - 10); // 设置最小宽度
        make.right.mas_lessThanOrEqualTo(self.view.mas_right).offset(-10);  // 设置距离右边最小距离
    }];
    
    _leftlbl = leftlbl;
    _rightLbl = rightLbl;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
//    NSLog(@"shouldChangeCharactersInRange:%@", textField.text);
    return YES;
}
- (void)textFieldDidChangeSelection:(UITextField *)textField{
//    NSLog(@"textFieldDidChangeSelection:%@", textField.text);
}
- (void)textFieldChanged:(UITextField *)textField{
    NSLog(@"textFieldChanged:%@", textField.text);
    if (textField.tag == 1) {
        _leftlbl.text = textField.text;
    }else{
        _rightLbl.text = textField.text;
    }
    
}

@end
